<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" 
		
	> 
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="banner类型" align="center" prop="name" />
      <el-table-column
      		   width="200"
      		   label="banner图" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
           <el-image
      		 fit='contain'
             style="width: 100px; height: 100px"
             :src="scope.row.previewFrist" 
             :preview-src-list="scope.row.previewList">
           </el-image>
      		   <el-button type="text">点图片查看全部banner图</el-button>
      		 </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
		<el-form-item label="banner类型" prop="pageType">
		  <el-select v-model="form.pageType"
		   class="width100"
		   placeholder="banner类型" clearable>
		    <el-option
		      v-for="item in pageType"
		      :key="item.type+''"
		      :label="item.name"
		      :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="banner图片">
			<FileUpload
				@input='inputFile'
				:value="fileList"
				:fileType="['png', 'jpg', 'jpeg']"
			>
			</FileUpload>
		</el-form-item>  
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
	listPageDecoration,
	delPageDecoration,
	insertPageDecoration,
	editPageDecoration,
	allListPageDecoration, 
	detailPageDecoration
} from "@/api/page/index";
import { strchangeArr } from "@/utils/index";
export default {
  name: "page",
  data() {
    return {
	  //上传图片
	  fileList:[],
	  previewFrist:[],
	  previewList:[],
	  //时间
	  courseTime:[],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pageType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  computed:{
	  pageType: {
	    get() {
	      return this.$store.state.common.pageType
	    },
	  },
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listPageDecoration(this.queryParams).then(response => {
        this.postList = response.rows.map(i=>{
			i.name = this.pageType.filter(v=>v.type == i.pageType)[0].name
			i.previewFrist = strchangeArr(i.images)[0];
			i.previewList = strchangeArr(i.images)
			return i
		})
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      };
	  this.fileList = []
	  this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
	  this.courseTime = []
      this.handleQuery();
    },
	//获取上传图片
	inputFile(file){
		this.form.images = file
	},
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加banner";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id
      this.form = row;
      this.open = true;
	  this.fileList = row.images
      this.title = "修改banner";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editPageDecoration(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertPageDecoration(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = {
		  id:row.id
	  };
      this.$modal.confirm('是否确认删除该banner？').then(function() {
        return delPageDecoration(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
